package com.tju.resource.oauth.controller.sys;

import com.tju.resource.model.SysVersions;
import com.tju.resource.server.SysVerService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SysVerController {

    public static final String prefix = "sysver";

    @Autowired
    SysVerService service;

    /**
     * version history list
     * */
    @PostMapping(prefix + "/list")
    public Result<Object> list() {
        Result<Object> res;

        try {
            List<SysVersions> sysVersions = service.verList();

            res = ResultUtil.success(ResultEnum.SUCCESS, "done.");
            res.setData(sysVersions);
        } catch (Exception e) {
            e.printStackTrace();
            res = ResultUtil.error(ResultEnum.UNKONW_ERROR, e.getMessage());
        }

        return res;
    }

    /**
     * new set version
     */
    @PostMapping(prefix + "/cur")
    public Result<Object> cur() {
        Result<Object> res;

        try {
            SysVersions sysVersions = service.curVer();

            res = ResultUtil.success(ResultEnum.SUCCESS, "done.");
            res.setData(sysVersions);
        } catch (Exception e) {
            e.printStackTrace();
            res = ResultUtil.error(ResultEnum.UNKONW_ERROR, e.getMessage());
        }

        return res;
    }

    /**
     * current version
     */
    @PostMapping(prefix + "/new")
    public Result<Object> newVer() {
        Result<Object> res;

        try {
            SysVersions sysVersions = service.newVer();

            res = ResultUtil.success(ResultEnum.SUCCESS, "done.");
            res.setData(sysVersions);
        } catch (Exception e) {
            e.printStackTrace();
            res = ResultUtil.error(ResultEnum.UNKONW_ERROR, e.getMessage());
        }

        return res;
    }

}
